package com.caojing.haolemai.ui.main;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.caojing.haolemai.R;
import com.caojing.haolemai.base.BaseActivity;
import com.caojing.haolemai.ui.cart.CartFragment;
import com.caojing.haolemai.ui.category.CategoryFragment;
import com.caojing.haolemai.ui.home.HomeFragment;
import com.caojing.haolemai.ui.mine.MineFragment;
import com.caojing.haolemai.ui.outlets.OutletsFragment;

import butterknife.Bind;
import butterknife.OnClick;
import cn.jpush.android.api.CustomPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;

public class MainActivity extends BaseActivity implements MainContract.View{
    @Bind(R.id.main_container)
    FrameLayout mainContainer;
    @Bind(R.id.main_home)
    ImageView mainHome;
    @Bind(R.id.main_category)
    ImageView mainCategory;
    @Bind(R.id.main_outlets)
    ImageView mainOutlets;
    @Bind(R.id.main_cart)
    ImageView mainCart;
    @Bind(R.id.main_mine)
    ImageView mainMine;
    private final int HOME = 0,CATEGORY=1,OUTLETS=2,CART=3,MINE=4;
    private HomeFragment homeFragment;
    private CategoryFragment categoryFragment;
    private OutletsFragment outletsFragment;
    private CartFragment cartFragment;
    private MineFragment mineFragment;
    private FragmentManager manager;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {

        CustomPushNotificationBuilder builder = new
                CustomPushNotificationBuilder(MainActivity.this,
                R.layout.customer_notitfication_layout,
                R.id.icon,
                R.id.title,
                R.id.text);
        // 指定定制的 Notification Layout
        builder.statusBarDrawable = R.mipmap.ic_launcher;
        // 指定最顶层状态栏小图标
        builder.layoutIconDrawable = R.mipmap.icon_brand;
        // 指定下拉状态栏时显示的通知图标
        JPushInterface.setPushNotificationBuilder(3, builder);

        manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        homeFragment = new HomeFragment();
        categoryFragment = new CategoryFragment();
        outletsFragment = new OutletsFragment();
        cartFragment= new CartFragment();
        mineFragment = new MineFragment();
        ft.add(R.id.main_container,homeFragment);
        ft.add(R.id.main_container,categoryFragment);
        ft.add(R.id.main_container,outletsFragment);
        ft.add(R.id.main_container,cartFragment);
        ft.add(R.id.main_container,mineFragment);
        ft.commit();

        selectFragment(HOME);

    }
    public void selectFragment(int position) {

        FragmentTransaction ft1 = manager.beginTransaction();
        mainHome.setSelected(false);
        mainCategory.setSelected(false);
        mainOutlets.setSelected(false);
        mainCart.setSelected(false);
        mainMine.setSelected(false);
        ft1.hide(homeFragment).hide(categoryFragment).hide(outletsFragment).hide(cartFragment).hide(mineFragment);
        switch (position){
            case HOME:
                ft1.show(homeFragment);
                mainHome.setSelected(true);
                break;
            case CATEGORY:
                ft1.show(categoryFragment);
                mainCategory.setSelected(true);
                break;
            case OUTLETS:
                ft1.show(outletsFragment);
                mainOutlets.setSelected(true);
                break;
            case CART:
                ft1.show(cartFragment);
                mainCart.setSelected(true);
                break;
            case MINE:
                ft1.show(mineFragment);
                mainMine.setSelected(true);
                break;
        }
        ft1.commit();
    }
    @OnClick({R.id.main_home, R.id.main_category, R.id.main_outlets, R.id.main_cart, R.id.main_mine})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_home:
                selectFragment(HOME);
                break;
            case R.id.main_category:
                selectFragment(CATEGORY);
                break;
            case R.id.main_outlets:
                selectFragment(OUTLETS);
                break;
            case R.id.main_cart:
                selectFragment(CART);
                break;
            case R.id.main_mine:
                selectFragment(MINE);
                break;
        }
    }
    @Override
    public void initData() {

    }
}
